import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/basic_department_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_response_card_entity.dart';

/// 应急处置卡
class EmergencyResourceCardController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? search;

  /// 部门
  final List<BasicDepartmentEntity> departments = [];
  BasicDepartmentEntity? selectedDepartment;

  int? totalCount;

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  @override
  void onInit() {
    super.onInit();
    getDepartments();
  }

  /// 获取部门
  Future<List<BasicDepartmentEntity>> getDepartments() async {
    final resp = await HttpUtil()
        .generateGetRequest<List<BasicDepartmentEntity>>(
          '${Interface.mainBaseUrl}/auth//basicConfiguration/getDepartmentTree',
        )
        .execute();
    resp.onSuccess((result) {
      departments.clear();
      departments.addAll(result.data ?? []);
      update();
    });
    return resp.data ?? [];
  }

  /// 分页
  Future<List<EmergencyResponseCardEntity>> getListData(int pageNum, int pageSize) async {
    final res = await HttpUtil().generateGetRequest<List<EmergencyResponseCardEntity>>(
      '${Interface.mainBaseUrl}/emergency//EmergencyCard/getEmergencyResponseCardPage',
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'departmentId': selectedDepartment?.id,
      },
    ).execute();
    res.onSuccess((result) {
      totalCount = res.pagination?.total;
    });
    return res.data ?? [];
  }
}
